/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d.archive;

import java.io.ByteArrayInputStream;
import java.util.Map;
import java.util.Properties;
import org.opu.vcforms.datasource.DataReceiver;
import org.opu.vcforms.datasource.Storage;
import org.opu.vcforms.template.archive.DocxArchiveTemplate;
import org.opu.vcforms.transform_t2d.content.ContentProcessor;
import org.opu.vcforms.transform_t2d.docx.Transformer;

/**
 * Convert archive that contain META-INF file and template.docx file in to
 * docx file. At first it replace text in the docx file with specific
 * smart-murkap, connected with discribing in META-INF file. Then, do the same
 * action as in {@link Transformer}
 * @author yura
 * @see DocxArchiveTemplate
 */
public class DocxTransformer extends Transformer {

    /**
     * @see Transformer#Transformer(DataReceiver, Map, Properties) 
     */
    public DocxTransformer(DataReceiver receiver, Map<String, Storage> settings,
            Properties systemSettings) {
        super(receiver, settings, systemSettings);
    }

    /**
     * @see Transformer#Transformer(DataReceiver, Map, Properties, ContentProcessor)
     */
    public DocxTransformer(DataReceiver receiver, Map<String, Storage> settings,
            Properties systemSettings, ContentProcessor contentProcessor) {
        super(receiver, settings, systemSettings, contentProcessor);
    }

    /**
     * In current situation, byte array is not a docx file. So, at first we
     * need to load {@link DocxArchiveTemplate}. Then we need to modifi
     * template.docx file. And at last we need to return representation of
     * docx file.
     */
    @Override
    protected Map<String, byte[]> toDocxArchive(byte[] v)
            throws IllegalArgumentException {

        try {
            DocxArchiveTemplate archive = new DocxArchiveTemplate();
            archive.load(new ByteArrayInputStream(v));

            Map<String, byte[]> docxFile = archive.getDocxFile();
            
            return docxFile;
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
}
